package com.putao.album.tag;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.putao.album.R;
import com.putao.album.base.BaseActivity;
import com.putao.album.widget.pttag.PtTag;
import com.putao.album.widget.pttag.PtTagListView;
import com.putao.album.widget.pttag.PtTagView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jidongdong on 15/4/28.
 * <p/>
 * 针对照片或相册的标签增删
 */
public class ActivityTagAdd extends BaseActivity {
    private TextView title_tv;
    private Button back_btn, right_btn;
    private EditText etx_tag;
    private PtTagListView tagview, all_tagview;
    private final List<PtTag> mTags = new ArrayList<PtTag>();
    private final String[] titles = new String[]{"yi", "2", "标签2", "四五里2", "四五里3", "四五里4", "四五里5", "四五里6", "四五里7", "四五里8", "四五里9", "四五里10"};


    @Override
    public int doGetContentViewId() {
        return R.layout.activity_tag_add;
    }

    @Override
    public void doInitSubViews(View view) {
        setPageTitle();
        back_btn = queryViewById(R.id.back_btn, true);
        right_btn = queryViewById(R.id.right_btn, true);
        back_btn.setBackgroundResource(R.drawable.all_button_close);
        right_btn.setBackgroundResource(R.drawable.all_button_ok);
        etx_tag = queryViewById(R.id.etx_tag);
        etx_tag.setOnEditorActionListener(mOnTagEditorListener);
        tagview = queryViewById(R.id.tagview);
        tagview.setDeleteMode(true);
        tagview.setTagViewBackgroundRes(R.drawable.tag_bg_tag);
        all_tagview = queryViewById(R.id.all_tagview);
        all_tagview.setTagViewTextColorRes(R.color.color_middle_grey);
        all_tagview.setTagViewBackgroundRes(R.drawable.tag_bg_white);
    }

    EditText.OnEditorActionListener mOnTagEditorListener = new EditText.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                doAddTag();
                return true;
            }
            return false;
        }
    };

    void doAddTag() {
        PtTag tag = new PtTag();
        tag.setId(mTags.size());
        tag.setChecked(true);
        tag.setTitle(etx_tag.getText().toString());
        mTags.add(tag);
        tagview.setTags(mTags, true);
        etx_tag.setText("");
    }

    void setPageTitle() {
        title_tv = queryViewById(R.id.title_tv);
        title_tv.setText(R.string.page_title_tag_add_del);
    }

    private void setUpData() {
        for (int i = 0; i < titles.length; i++) {
            PtTag tag = new PtTag();
            tag.setId(i);
            tag.setChecked(true);
            tag.setTitle(titles[i]);
            mTags.add(tag);
        }
    }

    @Override
    public void doInitData() {
        setUpData();
        tagview.setTags(mTags);
        all_tagview.setTags(mTags);
        tagview.setOnTagClickListener(new PtTagListView.OnTagClickListener() {
            @Override
            public void onTagClick(PtTagView tagView, PtTag tag) {
                tagview.removeTag(tag);
            }
        });

        all_tagview.setOnTagClickListener(new PtTagListView.OnTagClickListener() {
            @Override
            public void onTagClick(PtTagView tagView, PtTag tag) {
                etx_tag.setText(tag.getTitle());
                etx_tag.setSelection(etx_tag.getText().length());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.right_btn:
                //TODO:对相册或照片增删标签完成
                break;
        }
    }
}
